package ag.carnot.pinkblob.web.jsf.beans.pinkblob.finalizedesign;

import ag.carnot.web.jsf.common.structureddata.ComplexTypeWrapper;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.validator.ValidatorException;

import org.apache.log4j.Logger;

import pinkblob.entity.DbFile;
import pinkblob.event.DatabaseFileHandler;
import pinkblob.log.Log;

public class CompleteDesignBean {

	private static Logger log = new Log().getLogger(CompleteDesignBean.class);

	public void complete() {
		DatabaseFileHandler handler = new DatabaseFileHandler();

		// variables that have to be filled for completion
		String pid = (String) getProjectInformation().get("ProjectID");
		DbFile file = handler.getMailFlagFile(Integer.parseInt(pid));

		log.info("Pid " + pid + ", File " + file);

		// create message to display
		FacesMessage msg = new FacesMessage();
		String summary = "";
		if (pid == null) {
			summary = "Project ID not set or unknown!";
		}
		if (file == null) {
			summary = summary
					+ " You are required to select a file for presenting!";
		}
		if (!(summary.equals(""))) {
			log.error(summary);
			msg.setSummary(summary);
			throw new ValidatorException(msg);
		}

	}

	private ComplexTypeWrapper projectInformationWrapper = new ComplexTypeWrapper(
			"ProjectInformation", null);

	public ComplexTypeWrapper getProjectInformationWrapper() {
		return projectInformationWrapper;
	}

	@SuppressWarnings("unchecked")
	public Map getProjectInformation() {
		return (Map) projectInformationWrapper.getComplexType();
	}

	@SuppressWarnings("unchecked")
	public void setProjectInformation(Map projectInformation) {
		projectInformationWrapper = new ComplexTypeWrapper(
				projectInformationWrapper.getDataMappingId(),
				projectInformation);
	}
}
